package com.yksj.healthtalk.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.yksj.healthtalk.ui.R;

/**
 * 医生馆导航
 * 
 * @author Administrator
 * 
 */
public class DoctorNavigationPopAdapter extends BaseAdapter {

	private Context context;
	private HashMap<Integer, String> mIdPositionMap;// key ClickBtnId Integer
													// pop_List_Position
	private HashMap<Integer, String> mClickIdCodeMap;//key ClickBtnId value code hosiptalname 
	LinkedHashMap<String, String> map;
	private ArrayList<String> list;
	private int ClickBtnId = -1;
	private EditText mHospitalBtn;
	private Boolean mProvinceFlag;

	public DoctorNavigationPopAdapter(Context context, EditText mHospitalBtn) {
		this.context = context;
		mIdPositionMap = new HashMap<Integer, String>();
		mClickIdCodeMap = new HashMap<Integer, String>();
		map = new LinkedHashMap<String, String>();
		list = new ArrayList<String>();
		this.mHospitalBtn = mHospitalBtn;
	}

	public HashMap<Integer, String> getAllSelect() {
		// mPositionCodeMap.clear();
		return mIdPositionMap;
	}

	// 清空服务
	public void setSelectHospital() {
		cancelServiceData();
	}

	public int getClickBtnId() {
		return ClickBtnId;
	}
	
	public HashMap<Integer, String> getClickIdCodeMap(){
		return mClickIdCodeMap;
	}

	public void setClickBtnId(int ClickBtnId) {
		this.ClickBtnId = ClickBtnId;
	}

	/**
	 * 按钮的id 列表的position
	 * 
	 * @param ClickBtnId
	 * @param position
	 *            索引值 医院是 医院名
	 */
	public void setSelect(String position, int ClickId) {
//		// 筛选出城市使其单一
//		/******************************/
//		for (Map.Entry<Integer, String> e : mIdPositionMap.entrySet()) {
//			if (e.getKey() > 10 && ClickBtnId == 4) {
//				mIdPositionMap.remove(e.getKey());
//			}
//		}
//		/******************************/
		mIdPositionMap.put(ClickBtnId, position);//保存按钮ID 和 List的position 以便显示颜色
		ArrayList<String> key = new ArrayList<String>(map.values());
		if (ClickBtnId == 4) {//医院
			mClickIdCodeMap.put(ClickBtnId, position);
		}else {
			mClickIdCodeMap.put(ClickBtnId, key.get(Integer.valueOf(position)));
		}
		
		notifyDataSetChanged();
		// 按服务 和 按条件 清除对方数据
		switch (ClickId) {
		case 1:
		case 2:
			cancelConditionData();
			break;
		case 3:
		case 4:
		case 5:
		case 6:
			cancelServiceData();
			break;
		}
		if (ClickId > 10) {
			cancelServiceData();
		}
	}
	//清除服务的数据
	private void cancelServiceData() {
		Iterator<Integer> keys = mIdPositionMap.keySet().iterator();
		Integer key = -1;
		while (keys.hasNext()) {
			key = keys.next();
			if (key == 1 || key == 2) {
				keys.remove();
			}
		}
	}

	// 清除按条件的数据
	private void cancelConditionData() {
		Iterator<Integer> keys = mIdPositionMap.keySet().iterator();
		Integer key = -1;
		while (keys.hasNext()) {
			key = keys.next();
			if (key != 1 && key != 2) {
				keys.remove();
			}
		}
	}

	/**
	 * 按钮的id
	 */
	public void setClickBtnId(int ClickBtnId, LinkedHashMap<String, String> map) {
		if (map == null) {
			map = new LinkedHashMap<String, String>();
		}
		this.map.clear();
		this.map.putAll(map);
		list = new ArrayList<String>(map.keySet());
		this.ClickBtnId = ClickBtnId;
	}

	@Override
	public int getCount() {
		return map.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	public void setClickProvincePosition(int mProvincePosition,
			ListView mProvinceList) {
		if (mProvinceList.getTag() != null) {
			if ((Integer) mProvinceList.getTag() == mProvincePosition) {
				mProvinceFlag = true;
			} else {
				mProvinceFlag = false;
			}
		} else {
			mProvinceFlag = false;
		}
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TextView tv = new TextView(context);
		if (ClickBtnId != 3) {
			tv.setPadding(25, 20, 5, 20);
		}else {
			tv.setGravity(Gravity.CENTER);
		}
		
		tv.setTextSize(15);
		// 4 为医院
		 if (ClickBtnId != -1&&ClickBtnId != 4) {
		if (mIdPositionMap.containsKey(ClickBtnId)) {
			if (ClickBtnId == 3) {
				if (mProvinceFlag == true) {
					int po = Integer.valueOf(mIdPositionMap.get(ClickBtnId));
					if (po == position) {
						tv.setTextColor(context.getResources()
								.getColorStateList(R.color.tab_txt_orange));
					} else {
						tv.setTextColor(Color.BLACK);
					}
				} else {
					tv.setTextColor(Color.BLACK);
				}
			} else {
				int po = Integer.valueOf(mIdPositionMap.get(ClickBtnId));
				if (po == position) {
					tv.setTextColor(context.getResources().getColorStateList(R.color.tab_txt_orange));
				} else {
					tv.setTextColor(Color.BLACK);
				}
			}
		}}
		tv.setText(list.get(position));
		return tv;
	
	}
}
